package org.amicofragile.simpledsl

import java.util.List;

class Optimizer {
	List process(List list){
		def res = []
		def buf = []
		def currentGroup = null

		for(item in list){
			def group = criteria(item)
			if(currentGroup == null){
				currentGroup = group
			}
			if(group == currentGroup) {
				buf << item
			} else {
				res += processGroup(currentGroup, buf)
				buf = [item]
			}
			currentGroup = group
		}
		res + processGroup(currentGroup, buf)
	}
	
	def processGroup(group, buf){
		(groups[group] ?: groups.other)(buf)
	}
}
